/**
 * 一次计量设置参数弹窗
 * @author xiaoxujs
 */

import { Form, Input, InputNumber, message, Modal } from "antd";
import { FC, useEffect, useState } from "react";
import { ITlplTLaneQueryTarePhotoParam } from "./type";
import { postTlplTLaneTarePhotoParamUpdate } from "./api";

const OneJLSetParamsModule: FC<{
  open: boolean,
  handleSetLEDCallback: () => void;
  clickData?: ITlplTLaneQueryTarePhotoParam,
  laneId?: string;
}> = ({
  open,
  handleSetLEDCallback,
  clickData,
  laneId,
}) => {

    const [form] = Form.useForm<ITlplTLaneQueryTarePhotoParam>();

    const [loading, setLoading] = useState(false);

    useEffect(() => {
      if (clickData) {
        form.setFieldsValue(clickData)
      }
    }, [clickData, form])
    const handleOk = async () => {
      const values = await form.validateFields();
      if (clickData && laneId) {
        setLoading(true);
        const { code, msg } = await postTlplTLaneTarePhotoParamUpdate({
          ...values,
          id: laneId
        }) as { code: number; msg: string };
        if (code === 20000) {
          form.resetFields();
          handleSetLEDCallback();
        } else {
          message.error(msg)
        }
        setLoading(false)

      }

    };

    const handleCancel = () => {
      handleSetLEDCallback();
    };
    return <Modal title="设置一次计量参数" confirmLoading={loading} open={open} onOk={handleOk} onCancel={handleCancel}>
      <Form
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 14 }}
        form={form}
      >
        <Form.Item<ITlplTLaneQueryTarePhotoParam>
          label="设备IP地址"
          name="tarePhotoIp"
          rules={[{ required: true, message: '请输入!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<ITlplTLaneQueryTarePhotoParam>
          label="设备SDK端口号"
          name="tarePhotoPort"
          rules={[{ required: true, message: '请输入!' }]}
        >
          <InputNumber style={{ width: '100%' }} min={0} max={65535} />

        </Form.Item>
        <Form.Item<ITlplTLaneQueryTarePhotoParam>
          label="设备SDK用户名"
          name="tarePhotoUsername"
          rules={[{ required: true, message: '请输入!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<ITlplTLaneQueryTarePhotoParam>
          label="设备SDK密码"
          name="tarePhotoPassword"
          rules={[{ required: true, message: '请输入!' }]}
        >
            <Input />
        </Form.Item>
      </Form>
    </Modal>
  }

export default OneJLSetParamsModule;